package com.gamerent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gamerent.entity.GameAccount;
import com.gamerent.mapper.GameAccountMapper;
import com.gamerent.service.GameAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 游戏账号服务实现类
 */
@Service
public class GameAccountServiceImpl implements GameAccountService {

    @Autowired
    private GameAccountMapper gameAccountMapper;

    @Override
    public IPage<GameAccount> findByPage(Page<GameAccount> page, String gameName, Long categoryId, Integer status) {
        QueryWrapper<GameAccount> wrapper = new QueryWrapper<>();

        if (gameName != null && !gameName.trim().isEmpty()) {
            wrapper.like("game_name", gameName);
        }

        if (categoryId != null) {
            wrapper.eq("category_id", categoryId);
        }

        if (status != null) {
            wrapper.eq("status", status);
        }

        wrapper.orderByDesc("create_time");

        return gameAccountMapper.selectPage(page, wrapper);
    }

    @Override
    public GameAccount findById(Long id) {
        return gameAccountMapper.selectById(id);
    }

    @Override
    public GameAccount create(GameAccount gameAccount) {
        gameAccount.setRentCount(0);
        gameAccount.setStatus(1); // 默认可租状态
        gameAccountMapper.insert(gameAccount);
        return gameAccount;
    }

    @Override
    public GameAccount update(GameAccount gameAccount) {
        gameAccountMapper.updateById(gameAccount);
        return gameAccountMapper.selectById(gameAccount.getId());
    }

    @Override
    public boolean delete(Long id) {
        return gameAccountMapper.deleteById(id) > 0;
    }

    @Override
    public List<GameAccount> findHotAccounts(Integer limit) {
        QueryWrapper<GameAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1) // 只查询可租的账号
                .orderByDesc("rent_count")
                .last("LIMIT " + limit);

        return gameAccountMapper.selectList(wrapper);
    }

    @Override
    public IPage<GameAccount> search(Page<GameAccount> page, String keyword, String gameName, Long categoryId,
            Double minPrice, Double maxPrice) {
        QueryWrapper<GameAccount> wrapper = new QueryWrapper<>();

        if (keyword != null && !keyword.trim().isEmpty()) {
            wrapper.and(w -> w.like("game_name", keyword)
                    .or().like("account_name", keyword)
                    .or().like("description", keyword)
                    .or().like("tags", keyword));
        }

        if (gameName != null && !gameName.trim().isEmpty()) {
            wrapper.like("game_name", gameName);
        }

        if (categoryId != null) {
            wrapper.eq("category_id", categoryId);
        }

        if (minPrice != null) {
            wrapper.ge("daily_price", minPrice);
        }

        if (maxPrice != null) {
            wrapper.le("daily_price", maxPrice);
        }

        wrapper.eq("status", 1) // 只搜索可租的账号
                .orderByDesc("rent_count")
                .orderByDesc("create_time");

        return gameAccountMapper.selectPage(page, wrapper);
    }
}